package cn.itcast.bos.service.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.Dao.base.ICourierDao;
import cn.itcast.bos.Dao.base.IFixedareaDao;
import cn.itcast.bos.Dao.base.ITakeTimeDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;

/** 
 * 类说明
 *
 * @author Shao-Fei Fan 
 * @version 1.0,2017年9月6日 下午10:12:36 
 */
@Service
@Transactional
public class FixedareaServiceImpl implements IFixedareaService {
	
	// 得到持久层
	@Autowired
	private IFixedareaDao fixedareDao;
	
	// 得到taketime及courier的持久层
	@Autowired
	private ITakeTimeDao takeTimeDao;
	
	@Autowired
	private ICourierDao courierDao;
	
	/**
	 * @see cn.itcast.bos.service.base.IFixedareaService#save(FixedArea)
	 */
	@Override
	public void save(FixedArea model) {
		fixedareDao.save(model);
	}
	
	/**
	 * @see cn.itcast.bos.service.base.IFixedareaService#search(Specification, Pageable)
	 */
	@Override
	public Page<FixedArea> search(Specification<FixedArea> specification,
			Pageable pageable) {
		Page<FixedArea> page = fixedareDao.findAll(specification, pageable);
		return page;
	}
	
	/**
	 * @see cn.itcast.bos.service.base.IFixedareaService#associationCourierToFixedArea(FixedArea, Integer, Integer)
	 */
	@Override
	public void associationCourierToFixedArea(FixedArea model,
			Integer takeTimeId, Integer courierId) {
		
		// 通过hibernate的快照保存数据到数据库
		FixedArea fixedArea = fixedareDao.findOne(model.getId());
		Courier courier = courierDao.findOne(courierId);
		TakeTime takeTime = takeTimeDao.findOne(takeTimeId);
		
		// 关联上班时间到快递员
		courier.setTakeTime(takeTime);
		
		// 关联快递员到定区
		fixedArea.getCouriers().add(courier);
		
	}
	
	
	
}
